﻿<?php
class Cart extends CI_Controller
{
    function __construct() {
        parent::__construct();
        $this->load->model('sanpham_model');
    }
    function add_cart() {
        $product_id = $this->input->post('id');;
        $this->load->library('cart');
        $product = $this->sanpham_model->getOne($product_id);
        foreach ($this->cart->contents() as $rowid => $cart_item) {
            if ($product_id == $cart_item['id']) {
                $item = $cart_item;
                break;
            }
        }
        if (isset($item)) {
            // update cart
            $data = array(
                'rowid' => $item['rowid'],
                'qty' => $item['qty'] + 1
            );
            //echo '<pre>'; var_dump($data); echo '</pre>';die;
            $this->cart->update($data);
        } else {
            $data = array(
                'id' => $product['id'],
                'qty' => 1,
                'price' => $product['gia'],
                'name' => $product['masp'],
                'img' => $product['img'],
                'options' => array()
            );

            $this->cart->insert($data);
        }
    }
    function show_cart() {
        $this->load->library('image_lib');
        $data['cart'] = $this->cart->contents();
        //var_dump( $data['cart']);
        foreach($data['cart'] as $item) {
            $config['source_image']	=  base_url('assets/uploads/images') .'/'. $item['img'];
        }
        $config['image_library'] = 'gd2';
        $config['create_thumb'] = TRUE;
        $config['maintain_ratio'] = TRUE;
        $config['width']	 = 75;
        $config['height']	= 50;

        $this->load->library('image_lib', $config);

        $this->image_lib->resize();

        $data['total_all'] = $this->cart->total();
        $this->load->view('giohang',$data);
    }
    function remove() {
        $rowid = $this->input->post('rowid');
        $this->cart->update(array(
            'rowid' => $rowid,
            'qty' => 0
        ));
    }
    function ajax_update()
    {
        $rowid = $this->input->post('rowid');
        $data = array(
            'rowid' => $rowid,
            'qty' => $this->input->post('quantity')
        );

        $this->cart->update($data);
        $return = array(
            'status' => true,
            'subtotal' => $this->cart->_cart_contents[$rowid]['subtotal'],
            'total_current' => $this->cart->total()
        );
        echo json_encode($return);
    }
    function delCart() {
        $this->cart->destroy();
    }
    function donhang() {
		$this->load->library('email');
		$config = array (
			'smtp_host' => 'mail.it.vn',
			'smtp_user' => 'tmtv@it.vn',
			'smtp_pass' => '#462012',
			'protocol' => 'smtp',
			'charset'  => 'utf-8',
			'mailtype' => 'html',
			'wordwrap' => false
		);

		$this->email->initialize($config);
		
        $post = $this->input->post();
        $data = $post;
		$data['total_all'] = $this->cart->total();
		$data['cart'] = $this->cart->contents();
        $data['code'] = substr(md5(rand()), 0, 7);
		$cart = $this->cart->contents();
		$subject = 'Ваш заказ '.$data['code'].' на ассортимент Тинго выполнено, спасибо Вам...';
		$content = $this->load->view("mail-view", $data, true);;
        $email = 'phuong@it.vn';
		$this->email->from('tingo-shoes@mail.ru', 'Tingo-Shoes');
		$this->email->to($data['email']); 
		//$this->email->cc('tingo-shoes@mail.ru'); 
		$this->email->bcc('phuong@it.vn'); 

		$this->email->subject($subject);
		$this->email->message($content);	
		
		$this->email->send();
		
		$data = array(
		   'name' => $data['name'] ,
		   'email' => $data['email'] ,
		   'phone' => $data['phone'],
		   'content' => $data['content'],
		   'subject' => $subject,
		   'body' => $content,
		   'code' => $data['code'],
		   'created' => date('Y-m-d H:i:s')
		);
		$this->db->insert('order', $data); 
		
		$this->cart->destroy();
    }
}
?>